package com.yh.workbill.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yh.workbill.R;
import com.yh.workbill.databinding.ItemHomeworkbillBinding;
import com.yh.workbill.model.WorkBill;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

public class HomeWorkBillAdapter extends RecyclerView.Adapter<HomeWorkBillAdapter.ViewHolder> {

    public List<WorkBill> workBills;
    private OnItemClick mOnItemClick;

    public void setmOnItemClick(OnItemClick mOnItemClick) {
        this.mOnItemClick = mOnItemClick;
    }

    public void setWorkBills(List<WorkBill> workBills) {
        this.workBills = workBills;
        notifyDataSetChanged();
    }

    public HomeWorkBillAdapter(List<WorkBill> workBills) {
        this.workBills = workBills;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemHomeworkbillBinding inflate = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_homeworkbill, parent, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        WorkBill item = workBills.get(position);
        holder.getBinding().setData(item);
        holder.itemView.setOnClickListener(v -> {
            if (mOnItemClick != null) {
                mOnItemClick.onItemClick(item, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return workBills.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ItemHomeworkbillBinding binding;

        public ViewHolder(@NonNull ItemHomeworkbillBinding itemView) {
            super(itemView.getRoot());
            this.binding = itemView;
        }

        public ItemHomeworkbillBinding getBinding() {
            return binding;
        }
    }

    public interface OnItemClick {
        void onItemClick(WorkBill workBill, int position);
    }
}
